package mini.reanimate;

import static bogus.core.BogusCore.egl;

import java.awt.Font;
import java.awt.Point;
import java.util.ArrayList;

import com.jogamp.newt.event.MouseEvent;

import audio.Audio;
import audio.Clip;
import bogus.userInput.Input;
import owg.util.Calc;
import owg.util.euclidian.Compass;
import owg.util.opengl.BlendMode;
import owg.util.opengl.ColorF;
import owg.util.opengl.TTFFontRenderer;
import graphics.Shape;
import hubol.HMinigame;
import hubol.Shake;
import hubol.Sprite;

public class Reanimate extends HMinigame{
	public static Sprite sand = new Sprite("sanddrawing");
	public static Reanimate me;
	
	public final Sprite inventory = new Sprite("inventory"), click = new Sprite("click"), mouse = new Sprite("mouse"), sStick = new Sprite("sstick"), sPetals = new Sprite("sflower"), sMoney = new Sprite("money"), sFlame = new Sprite("sflame");
	private final Point inventoryPosition = new Point(432, 454);
	
	private final Font myFont = TTFFontRenderer.getFont("georgiai").deriveFont(0, 24);
	private final TTFFontRenderer font = new TTFFontRenderer(myFont, new ColorF(1, 1, 1, .78f));
	private final Point captionPosition = new Point(432, 96);
	
	private final ColorF darkdark = new ColorF(ColorF.rgbToFloat(0x353535));
	
	private String caption = "";
	private int captionTimer = 0;
	
	private ArrayList<Shake> s = new ArrayList<Shake>();
	
	public boolean stick = false;
	public boolean gotStick = false;
	public int petals = 0;
	public int petalsPlaced = 0;
	public boolean gotMoney = false;
	public boolean spentMoney = false;
	public boolean money = false;
	public boolean flame = false;
	public boolean written = false;
	public boolean[] gotPetal = new boolean[]{false, false, false};
	public boolean HUD = true;
	
	public Screen level = null;
	public Left LEFT;
	public Right RIGHT;
	public Writing WRITING;
	public Flame FLAME;
	public Only ONLY;
	
	public Bogus BOGUS;
	
	private int mouseImage = 4;
	private Sprite mouseSprite = null;
	
	public boolean BUSY = false;
	
	private final float scale = .5f, scal = .4f;
	public PPoint pStick = new PPoint((int)(inventoryPosition.x - (192 * scale)), inventoryPosition.y), pPetals = new PPoint((int)(inventoryPosition.x - (64 * scale)), inventoryPosition.y), pMoney = new PPoint((int)(inventoryPosition.x + (64 * scale)), inventoryPosition.y), pFlame = new PPoint((int)(inventoryPosition.x + (192 * scale)), inventoryPosition.y);
	private final PPoint pgStick = pStick.getLocation(), pgPetals = pPetals.getLocation(), pgMoney = pMoney.getLocation(), pgFlame = pFlame.getLocation();
	
	private Clip[] music;
	private double[] volume, goal;
	
	@Override
	public void initImpl() {
		System.out.println(HMinigame.me);
		me = this;
		//new Dance();
		
		egl.setBlendMode(BlendMode.NORMAL_PRE);
		
		click.setOrigin(Compass.NORTHWEST);
		mouse.setOrigin(Compass.NORTHWEST);
		
		for (int i=0; i<16; i++)
			s.add(new Shake());
		
		LEFT = new Left();
		RIGHT = new Right();
		WRITING = new Writing();
		FLAME = new Flame();
		ONLY = new Only();
		BOGUS = new Bogus(this);
		
		Clip[] temp = new Clip[]{Audio.getClip("musicr"), Audio.getClip("musicl")};
		music = new Clip[4 + temp.length];
		volume = new double[music.length];
		goal = new double[music.length];
		
		for (int i=0; i<4; i++){
			music[i] = Audio.getClip("music"+i);
			volume[i] = 0;
			goal[i] = 0;
		}
		
		for (int i=0; i<temp.length; i++){
			music[4 + i] = temp[i];
			volume[i] = 0;
			goal[i] = 0;
		}
		
		for (Clip m : music)
			m.loop();
		
		on(0);
		
		//setLevel(LEFT);
		setLevel(ONLY);
	}
	
	public void bogusLand(){
		level.bogusLand();
	}
	
	public void on(int i){
		goal[i] = 1;
	}
	public void off(int i){
		goal[i] = 0;
	}
	
	public void stop(){
		for (Clip m : music)
			m.stop();
	}
	
	public void pitch(double p){
		for (Clip m : music)
			m.setSpeed(null, p);
	}
	
	public void point(PPoint p){
		p.x = Input.mouseX;
		p.y = Input.mouseY;
	}
	
	public void approach(PPoint p, PPoint q){
		p.x = Calc.approach(p.x, q.x, 7);
		p.y = Calc.approach(p.y, q.y, 7);
	}
	
	public class PPoint{
		public double x, y;
		
		public PPoint(double x, double y){
			this.x = x;
			this.y = y;
		}
		
		public PPoint getLocation(){
			return new PPoint(x, y);
		}
	}
	
	public void setLevel(Screen s){
		if (level != null)
			level.leave();
		level = s;
		level.enter();
	}
	
	public void setMouse(int mouse){
		mouseImage = mouse;
	}
	
	public void setMouse(Sprite mouse){
		mouseSprite = mouse;
	}
	
	public void step(){
		approach(pStick, pgStick);
		approach(pPetals, pgPetals);
		approach(pMoney, pgMoney);
		approach(pFlame, pgFlame);
		
		super.step();
		
		for (int i=0; i<music.length; i++)
			music[i].setPosition(null, volume[i] = Calc.approach(volume[i], goal[i], 10));
		
		mouseImage = 4;
		mouseSprite = null;
		if (level != null)
			level.step();
		BOGUS.step();
		
		if (captionTimer > 0)
			captionTimer -= 1;
		
		quitStep();
	}
	
	public void render(){
		egl.clearScreen(ColorF.LTGRAY);
		
		boolean able = level != null;
		boolean shadow = true;
		
		if (able)
			shadow = level.background();
		
		BOGUS.render(shadow);
		super.render();
		
		if (able)
			level.foreground();
		
		if (captionTimer > 0){
			egl.setColor(darkdark);
			egl.setAlpha(.6f);
			
			double w = font.getStringWidth(caption) + 32, h = font.getStringHeight(caption) + 32;
			for (int i=0; i<3; i++)
				Shape.drawRectangle((float)(captionPosition.x - (w / 2) + s.get((i * 2)).x), (float)(captionPosition.y - (h / 2) + s.get((i * 2)).y), (float)(captionPosition.x + (w / 2) + s.get((i * 2) + 1).x), (float)(captionPosition.y + (h / 2) + s.get((i * 2) + 1).y));
			
			egl.setColor(ColorF.WHITE);
			for (int i=0; i<3; i++){
				font.begin();
				font.draw(caption, (float)(captionPosition.x + s.get(6 + i).x), (float)(captionPosition.y + s.get(6 + i).y - 6), Compass.CENTER);
				font.end();
			}
		}
		
		egl.setColor(ColorF.WHITE);
		
		if (HUD){
			inventory.render(1, inventoryPosition.x + s.get(14).x, inventoryPosition.y + s.get(14).y, scale, scale, 0);
			inventory.render(0, inventoryPosition.x + s.get(9).x, inventoryPosition.y + s.get(9).y, scale, scale, 0);
			if (stick)
				sStick.render(0, pStick.x + s.get(10).x, pStick.y + s.get(10).y, scal, scal, 0);
			if (petals > 0)
				sPetals.render(petals - 1, pPetals.x + s.get(11).x, pPetals.y + s.get(11).y, scal, scal, 0);
			if (money)
				sMoney.render(0, pMoney.x + s.get(12).x, pMoney.y + s.get(12).y, scal, scal, 0);
			if (flame)
				sFlame.render(0, pFlame.x + s.get(13).x, pFlame.y + s.get(13).y, scal, scal, 0);
		}
		
		if (!BUSY){
			Sprite m = mouse;
			int i = mouseImage;
			if (i == 4 && Input.checkButton(MouseEvent.BUTTON1)){
				m = click;
				i = 0;
			}
			if (mouseSprite != null){
				m = mouseSprite;
				i = 0;
				if (m == sPetals){
					i = petals - 1;
					if (i < 0){
						m = mouse;
						i = 4;
					}
				}
			}
			m.render(i, Input.mouseX + s.get(15).x, Input.mouseY + s.get(15).y);
		}
		
		quitRender();
	}
	
	public void caption(String caption, int captionTimer){
		this.caption = caption;
		this.captionTimer = captionTimer;
	}

	public void dispose(){
		s = null;
		super.dispose();
		sand = null;
		me = null;
	}
	
	@Override
	public int getFps() {
		return 60;
	}

	@Override
	public void completeMinigameImpl() {
		// TODO Auto-generated method stub
		
	}

}
